import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_core_widget/widgets/keepalive/kq_keep_alive.dart';
import 'package:kq_flutter_core_widget/widgets/stackView/stack_view.dart';
import 'package:kq_flutter_widgets/widgets/button/kq_small_button.dart';
import 'package:kq_flutter_widgets/widgets/titleBar/kq_title_bar.dart';

class StackViewDemo extends StatefulWidget {
  const StackViewDemo({super.key});

  @override
  State<StatefulWidget> createState() => StackViewDemoState();
}

class StackViewDemoState extends State<StackViewDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KqHeadBar(
        headTitle: 'StackView演示',
        back: () {
          Get.back();
        },
      ),
      body: StackView(
        init: (state) {
          return Column(
            children: [
              const Text("我是首页"),
              KqSmallButton(
                title: "打开新页面",
                onTap: (disabled) {
                  state.addWidget(TestPage1(state: state));
                },
              ),
              Expanded(
                  child: Container(
                color: Colors.purple,
              )),
            ],
          );
        },
      ),
    );
  }
}

class TestPage1 extends StatelessWidget {
  final StackViewState state;

  const TestPage1({super.key, required this.state});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        const Text("我是TestPage1"),
        KqSmallButton(
          title: "返回",
          onTap: (disabled) {
            state.back();
          },
        ),
        KqSmallButton(
          title: "打开新页面",
          onTap: (disabled) {
            state.addWidget(TestPage2(state: state));
          },
        ),
        Expanded(
            child: Container(
          color: Colors.amber,
        )),
      ],
    );
  }
}

class TestPage2 extends StatelessWidget {
  final StackViewState state;

  const TestPage2({super.key, required this.state});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        const Text("我是TestPage2"),
        KqSmallButton(
          title: "返回",
          onTap: (disabled) {
            state.back();
          },
        ),
        KqSmallButton(
          title: "打开新页面",
          onTap: (disabled) {
            state.addWidget(KqKeepAlive(child: TestPage3(state: state)));
          },
        ),
        Expanded(
            child: Container(
          color: Colors.cyan,
        )),
      ],
    );
  }
}

class TestPage3 extends StatefulWidget {
  final StackViewState state;

  const TestPage3({super.key, required this.state});

  @override
  State<StatefulWidget> createState() => _TestPage3State();
}

class _TestPage3State extends State<TestPage3> {
  @override
  void initState() {
    super.initState();
    print(">>>>>>>>>>>>>>>>>>>>>>>>>");
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        const Text("我是TestPage3"),
        KqSmallButton(
          title: "返回",
          onTap: (disabled) {
            widget.state.back();
          },
        ),
        Expanded(
            child: Container(
          color: Colors.blueAccent,
        )),
      ],
    );
  }

  @override
  void dispose() {
    super.dispose();
    print("+++++++++++++++++++++");
  }
}
